﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ClassMeta.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.MetaData
{
  using System;
  using System.Collections.Generic;
  using System.Linq;

  /// <summary>
  /// The class meta.
  /// </summary>
  public class ClassMeta
  {
    #region Public Properties

    /// <summary>
    /// The accessors for obtainment (agent instrumentation)
    /// </summary>
    public IList<AccessorMeta> Accessors { get; internal set; }

    /// <summary>
    /// Plaintext description.
    /// </summary>
    public string Description { get; internal set; }

    /// <summary>
    /// If the class has <see cref="Accessors"/> or not.
    /// </summary>
    public bool HasAccessors { get; internal set; }

    /// <summary>
    /// Points to the index <see cref="PropertyMeta"/>.
    /// </summary>
    /// <remarks>
    /// Indexes are ordered by it's <see cref="PropertyMeta.SubOid"/>.
    /// </remarks>
    public PropertyMeta[] Indexes { get; internal set; }

    /// <summary>
    /// If this class represents a SNMP table or a set of scalars.
    /// </summary>
    public bool IsTable { get; internal set; }

    /// <summary>
    /// The base oid.
    /// </summary>
    public string Oid { get; internal set; }

    /// <summary>
    /// Managed properties.
    /// </summary>
    public IList<PropertyMeta> Properties { get; internal set; }

    /// <summary>
    /// Gets the reflected type.
    /// </summary>
    public Type ReflectedType { get; internal set; }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// Gets an <see cref="PropertyMeta"/> by it's <see cref="PropertyMeta.SubOid"/> value.
    /// </summary>
    /// <param name="relativeOid">
    /// The <see cref="PropertyMeta.SubOid"/> to search for.
    /// </param>
    /// <returns>
    /// An <see cref="PropertyMeta"/> matching the inparam relativeOid.
    /// </returns>
    /// <exception cref="Exception">
    /// If not found.
    /// </exception>
    public PropertyMeta GetPropertyMetaByOid(int relativeOid)
    {
      return (from property in this.Properties where property.SubOid == relativeOid select property).First();
    }

    /// <summary>
    /// Gets an <see cref="PropertyMeta"/> by it's <see cref="PropertyMeta.SubOid"/> value.
    /// </summary>
    /// <param name="relativeOid">
    /// The <see cref="PropertyMeta.SubOid"/> to search for.
    /// </param>
    /// <param name="value">
    /// An <see cref="PropertyMeta"/> matching the inparam relativeOid. If not found it will be null.
    /// </param>
    /// <returns>
    /// It returns true if succeeded to obtain a <see cref="PropertyMeta"/> from the <paramref name="relativeOid"/>.
    /// </returns>
    public bool TryGetPropertyMetaBySubOid(int relativeOid, out PropertyMeta value)
    {
      value = (from property in this.Properties where property.SubOid == relativeOid select property).FirstOrDefault();

      return null != value;
    }

    #endregion
  }
}